/*
 * =====================================================================================
 *
 *       Filename:  ip-mac-conv.c
 *
 *    Description:  ip and mac convertion
 *
 *        Version:  1.0
 *        Created:  2020年02月25日 11时02分46秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Yuan Yulei (yyl), yyl_nudt@qq.com
 *   Organization:  National University Defense of Technology
 *
 * =====================================================================================
 */

#include <stdint.h>
#include <stdio.h>

/*
 * convert ip of no more than 16 byte string separated by char '.' to ip of 4 byte
 * input:
 *  char strIP[16]: ip of no more than 16 byte string separated by char '.'
 * output:
 *  uint8_t ip[4]: ip of 4 byte
 * return 0 if function success
 */
int 
str2ip(uint8_t ip[4], char strIP[16]);


/*
 * convert ip of 4 byte to string of no more than 16 byte, every field separated by char '.'
 * input:
 *  uint8_t ip[4]: ip of 4 byte
 * output:
 *  char str[16]: ip of no more than 16 byte, every field separated by char '.'
 * return 0 if function success
 */
int
ip2str(char str[16], uint8_t ip[4]);
  
/*
 * convert ip of no more than 16 byte string separated by char '.' to ip of 4 byte 
 * input:
 *  char strIP[16]: ip of no more than 16 byte string separated by char '.'
 * output:
 *  uint8_t ip[4]: ip 
 */
int
ipstr2ip(uint8_t ip[4], char strIP[16]);
 
/*
 * convert ip of no more than 16 byte string separated by char '.' to int
 * input:
 *  char strIP[16]: ip of no more than 16 byte string separated by char '.'
 * output:
 *  uint32_t* lip: int of ip
 */
int
ipstr2int(uint32_t* lip, const char strIP[16]);
 
/*
 * convert ip of 4 byte to int
 * input:
 *  uint8_t pip[4]: ip of 4 byte
 * ourput:
 *  uint32_t* lip: return the int of ip
 */
int 
ip2int(uint32_t* lip, uint8_t pip[4]);
 
/*
 * convert ip of int to ip of 4 byte
 * input:
 *  uint32_t ip_int: ip of int
 * output:
 *  uint8_t ip[4]: ip of 4 byte
 */
int
int2ip(uint8_t ip[4], uint32_t ip_int);

/*
 * convert ip of int to ip of no more than 16 byte string
 * input :
 *  uint32_t ip_int: ip of int
 * output:
 *  char ip_str[16]: ip of no more than 16 byte string
 */
int
int2ipstr(char ip_str[16], uint32_t ip_int);

/*
 * 将6Byte的mac地址转换为':'隔开的字符串
 * input: 
 *       uint8_t pMac[6]: 6Byte表示的mac地址
 * output:
 *       char szMac[32]: 字符串表示的mac地址
 */
int 
mac2str(char szMac[32], uint8_t pMac[6], const char chKen/* = ':'*/);

/*
 * 将6Byte的mac地址转换为8Byte的整型数
 * input:
 *       uint8_t pMac[6]: 6Byte表示的mac地址
 * output:
 *       uint64_t lMac: mac地址的8Byte整型数,各字节左移实现
 */
int
mac2int(uint64_t* lMac, uint8_t pMac[6]);

/*
 * 将8Byte整型数表示的mac地址转换为6Byte数组表示的mac地址
 * input:
 *       uint64_t lMac: 8Byte整型数表示的mac地址
 * output:
 *       uint8_t pMac[6]: 6Byte数组表示的mac地址
 */
	int
int2mac(uint8_t pMac[6], uint64_t lMac);

/*
 * 将字符串表示的mac地址转换为6Byte
 * input: 
 *       const char *szMac: 字符串表示的mac地址
 * output:
 *       uint8_t pMac[6]: 6Byte表示的mac地址
 */
int 
str2mac(uint8_t pMac[6], const char * szMac);


